var pr_ajax = null;

function get_id_from_value(value){
	return value.substring(0, value.indexOf(' '));
}

function get_name_from_value(value){
	return value.substring(value.indexOf(' ')+1, value.length);
}

function set_category_check(parent_id, category_list, check){
	var surfix_id = '_' + parent_id;
	for(var i=0; i<category_list.length; i++){
		id = category_list[i].id;
		index = id.indexOf(surfix_id);
		if (index > 0 && index == id.length - surfix_id.length){		
			category_list[i].checked = check;
			// Get real id of this category and do check for its children
			splitted_id = get_id_from_value(category_list[i].value);
			set_category_check(splitted_id, category_list, check);
		}
	}
}

// Event when click on a category checkbox
function click_checkbox(cat_id, field_name){	
	var category_frm = document.forms['form_'+field_name];
	var category_list = category_frm['filter_'+field_name+'[]'];
	for(var i=0; i<category_list.length; i++)
		if (get_id_from_value(category_list[i].value) == cat_id){
			check = category_list[i].checked;
			break;
		}
	// Set check or uncheck for children of current checkbox
	set_category_check(cat_id, category_list, check);
		
}

function delete_active(itemid,field_name,id)
{
	div_object = document.getElementById(id);
	div_object.innerHTML='';
	
	if (parent[field_name+'_'+itemid])
		var object = document.getElementById('filter_'+field_name+'_list'+itemid+'_'+parent[field_name+'_'+itemid]);
	else
		var object = document.getElementById('filter_'+field_name+'_list'+itemid);
		
	
	if(object) object.checked = 0;
	click_checkbox(itemid,field_name);
	doSearchs();
}

function delete_text_active(field_name,id)
{
	div_object = document.getElementById(id);
	div_object.setHTML('');
	object = document.getElementById(field_name);
	object.value = '';
	doSearchs();
}

// Build data by search params from filters
function buildDatas(){		
	// Build data for categories
	var full_of_checked;
	var temp_data;	
	var temp_frm;
	var temp_list;
	var filter_data = '';
	var arr_cat_data = new Array();
	var label_active = new Array();
	
	if( typeof(filter) == "undefined" ){
		return "";		
	}
	
	//******************** UPDATE LOCATION, CATEGORY FILTER ********************
	for ( var j=0;j<filter.length;j++)
	{
		label_active[j] = '';
		temp_data = '';
		temp_frm = document.forms['form_'+filter[j]];
				
		if (temp_frm != null){
			temp_list = document.getElementsByName('filter_'+filter[j]+'[]');//category_frm['filter_'+filter[j]+'[]'];
			
			full_of_checked = true;
			var temp_count = 0;
			for(var i=0; i<temp_list.length; i++){
				
				//alert(temp_list[i].name + " = " + temp_list[i].id + " => " + temp_list[i].checked);
				
				//alert( temp_list[i].id + " => " + document.getElementById(temp_list[i].id).checked );
				
				if (temp_list[i].checked)
				{
					if (temp_data=='')
					{
						temp_data +=  get_name_from_value(temp_list[i].value);
						
					}
					else
					{
						temp_data += ',' + get_name_from_value(temp_list[i].value);
						
					}
					
					label_active[j] += ' <span style="width:auto;" id="'+filter[j]+'_active_'+temp_count+'">' + temp_list[i].title + '<span onclick="delete_active('+get_id_from_value(temp_list[i].value)+',\''+filter[j]+'\',\''+filter[j]+'_active_'+temp_count+'\');"> <img src="' + siteurl + 'modules/mod_jafilter_jobs/ja_filter_jobs/images/del.gif" alt="Delete" /></span></span>';
					//Get label
					arr_cat_data[temp_count] = temp_list[i];	
										
					temp_count++;					
				}		
				else{
					//full_of_checked = false;
				}
			}
			// alert(filter[j])
			 $('filter-active-'+filter[j]).setHTML(label_active[j]);
			if(label_active[j].trim()==''){
				$('filter-active-'+filter[j]).setStyle('display','none');
				$('filter-inactive-'+filter[j]).setStyle('display','block');
			}
			else{
				$('filter-inactive-'+filter[j]).setStyle('display','none');
				$('filter-active-'+filter[j]).setStyle('display','block');
			}
			
			//if (full_of_checked ) temp_data = '';
			
		}
		if(temp_data!=''){
			filter_data += '&'+filter[j]+ "=" + temp_data;
		}
	}

	
	//******************** END ********************
	//******************** UPDATE KEYWORD FILTER ********************
	if ($type($('filter_keyword')) && $type($('default_keyword')) && $('filter_keyword').value!=$('default_keyword').value)
	{		
		filter_data += '&filter_keyword='+$('filter_keyword').value;
//		label_active_keyword = ' <span style="width:auto;" id="keyword_active_label">' + $('filter_keyword').value + '<span onclick="delete_text_active(\'filter_keyword\',\''+'keyword_active_label'+'\');"> <img src="' + siteurl + 'modules/mod_jafilter_jobs/ja_filter_jobs/images/del.gif" alt="Delete" /></span></span>';
//		$('filter-inactive-keyword').style.display = 'none';
//		$('filter-active-keyword').style.display = 'block';
//		$('filter-active-keyword').innerHTML = label_active_keyword;
	}
	else
	{
//		$('filter-active-keyword').innerHTML = '&nbsp;';
//		$('filter-active-keyword').style.display = 'none';
//		$('filter-inactive-keyword').style.display = 'block';
	}
	//******************** END ********************
	
	//******************** UPDATE SALARY FILTER ********************
	if($type($('filter_salary_from'))){
		label_active_salary = '';
		if ($('filter_salary_from').value!='')
		{
			filter_data += '&filter_salary_from=' + $('filter_salary_from').value;
			label_active_salary = ' <span style="width:auto;" id="salary_from_active_label">' + $('filter_salary_from').value + '<span onclick="delete_text_active(\'filter_salary_from\',\''+'salary_from_active_label'+'\');"> <img src="' + siteurl + 'modules/mod_jafilter_jobs/ja_filter_jobs/images/del.gif" alt="Delete" /></span></span>';
		}
		if ($('filter_salary_to').value!='')
		{
			filter_data += '&filter_salary_to=' + $('filter_salary_to').value;
			label_active_salary += ' <span style="width:auto;" id="salary_to_active_label">' + $('filter_salary_to').value + '<span onclick="delete_text_active(\'filter_salary_to\',\''+'salary_to_active_label'+'\');"> <img src="' + siteurl + 'modules/mod_jafilter_jobs/ja_filter_jobs/images/del.gif" alt="Delete" /></span></span>';
		}
		if (label_active_salary!='')
		{					
			if ($type($('filter-inactive-salary')))
				$('filter-inactive-salary').setStyle('display','none');
				
			if ($type($('filter-active-salary')))
			{
				$('filter-active-salary').setStyle('display','block');
				$('filter-active-salary').setHTML(label_active_salary);
			}
		}
		else
		{
			
			if ($type($('filter-inactive-salary')))
				$('filter-inactive-salary').setStyle('display','block');
			if ($type($('filter-active-salary')))
			{				
				$('filter-active-salary').setStyle('display','none');
				$('filter-active-salary').setHTML('');
			}
		}
	}
	
	
	//******************** END ********************
	return filter_data;
}

function doSearchs(layout){
	if ($type($('filter_keyword')) && $type($('default_keyword')) && $('filter_keyword').value!=$('default_keyword').value)
	{
		if($('filter_keyword').value.trim()!='' && $('filter_keyword').value.length<3){
			alert('Minimum length of keyword must be at least 3 characters');
			$('filter_keyword').focus();
			return;
		}
	}
	
	var url = location.href;
	// Display loading icon	
	displayLoadingSpan();
	
	// Use AJAX to do search and reload listing area
	var filter_data = buildDatas();
	
	if($type(number_items_search)){
		filter_data += '&limit='+number_items_search;		
	}else{
		if($type($('limit'))){
			filter_data += '&limit='+$('limit').value;
		}
	}

	if((typeof layout)=='undefined'){
		layout = 'filter_job_list';
	}
		
	var mainUrl = siteurl+"index.php?tmpl=component&option=com_jajobboard&view=jajobs&ajax=1&doSearchs=1&Itemid=" + itemid + '&layout=' + layout;
	if($('filter_order')!=null){
		mainUrl += "&filter_order=" + escape($('filter_order').value) + '&filter_order_Dir=' + $('filter_order_Dir').value;
	}
	
	var urlrequest = mainUrl + filter_data;
	
	
	pos = url.indexOf('#');
	if(pos > 0) {
		queryStr = url.substr(pos);
	}else {
		queryStr = '';
	}

	url = url.replace(queryStr, '');
	
	var newUrl = url + '#Top'+ filter_data;
//	newUrl = ad_replace("option=","&",newUrl,"option=com_jajobboard&");
//		newUrl = ad_replace("&view","&",newUrl,"&view=jajobs&");
//		newUrl = ad_replace("&layout","&",newUrl,"&layout=jalist&");
	if($('filter_order')!=null){
		newUrl += "&filter_order=" + escape($('filter_order').value) + '&filter_order_Dir=' + $('filter_order_Dir').value;
	}
		
	if ($type($('jobboard-content'))){
		window.location = newUrl;
		new Ajax(urlrequest, {'method':'get', 'update': 'jobboard-content', 'onComplete': update}).request();
	}
	else{
		if(redirect_url.indexOf('?') <0) {
			redirect_url = redirect_url + '?';
		}
		window.location = redirect_url + filter_data;
	}
	
}
function reset(){		
	
	if($('filter_order')==null){
		$('filter_keyword').value = '';
		return;
	}
	
	for (i=0;i<filter.length;i++)
	{
		$('filter-active-' + filter[i]).setStyle('display','none');
		$('filter-inactive-' + filter[i]).setStyle('display','block');
	}
//	$('filter-active-cat_id').setStyle('display','none');
//	$('filter-inactive-cat_id').setStyle('display','block');
//	
//	$('filter-active-location_id').setStyle('display','none');
//	$('filter-inactive-location_id').setStyle('display','block');
	
	$('filter_keyword').value = '';
	
	if( $('filter-active-salary') ){
		$('filter-active-salary').setStyle('display','none');
		$('filter-inactive-salary').setStyle('display','block');
		$('filter_salary_from').value = '';
		$('filter_salary_to').value = '';
	}
	
	for (i=0;i<filter.length;i++)
	{
		object = document.getElementsByName('filter_' + filter[i]+'[]');
		if (object){
			for (j=0;j<object.length;j++){
				object[j].checked = 0;
			}
		}
	}
	// Use AJAX to do search and reload listing area
	var url = location.href;
	pos = url.indexOf('#');
	if(pos > 0) {
		queryStr = url.substr(pos);
	}else {
		queryStr = '';
	}

	url = url.replace(queryStr, '');
	window.location = url+'#Top';
	var mainUrl = siteurl+"index.php?tmpl=component&option=com_jajobboard&view=jajobs&ajax=1&doSearchs=1&Itemid=" + itemid + '&layout=filter_job_list&tmpl=component'; //+ layout;
	if($type(number_items_search)){
		mainUrl += '&limit='+number_items_search;
	}else{
		if($type($('limit'))){
			mainUrl += '&limit='+$('limit').value;
		}
	}
	
	
	if(pr_ajax != null){
		pr_ajax.cancel();
	}
	
	displayLoadingSpan();
	
	pr_ajax =new Ajax(mainUrl, {'method':'get', 'update': 'jobboard-content', 'onComplete': update}).request();
}

function update(request) {
	if ($('ja-loader'))
		$('ja-loader').setStyle ('display', 'none');
	
	var JTooltips = new Tips($$('#jobboard-content .hasTip'), { maxTitleChars: 50, fixed: false});
	if(typeof(document.adminForm)!=='undefined'){
		form = document.adminForm;
	}
}

function update_filter_active(id){
	
	var active_div = document.getElementById(id);
	active_div.setStyle('display','block');	
}

// end gam add
//ajax pagination
function ajaxPagination(url,divid) {	
	displayLoadingSpan();
	if(pr_ajax != null){
		pr_ajax.cancel();
	}
	pr_ajax = new Ajax(url,{method:'get', update:divid, onComplete:update}).request();
	
}


function displayLoadingSpan() {
	if(!$type($('ja-loader'))){
		var loader = new Element('div', {'id': 'ja-loader'});
		loader.setText('Loading...');
		if($type($('jobboard-content'))){
			loader.injectBefore($('jobboard-content'));
		}
		else{
			loader.injectInside(document.body);
		}
					
	}
	if($type($('ja-loader'))){
		$('ja-loader').setStyle('display', 'block');
	}
}

function gup( name )
{
  name = name.replace("/[\[]/","\\\[").replace("/[\]]/","\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
function show_div_filter(id,position)
{	
	if (id!='close')
	{
		if($type($('filter_salary_from'))){
			if (id!='div-filter-salary-'+position  && $type($('div-filter-salary-'+position)))
				$('div-filter-salary-'+position).setStyle('display','none');
		}
		for (i=0;i<filter.length;i++)
		{
			temp_id = 'div-filter-'+filter[i]+'-'+position;
			if (id!=temp_id)
				$(temp_id).setStyle('display','none');
		}		
		if ($(id).style.display != 'block')
			$(id).setStyle('display','block');
		else
			$(id).setStyle('display','none');
	}
	else
	{
		for (i=0;i<filter.length;i++)
		{
			temp_id = 'div-filter-'+filter[i]+'-'+position;
			$(temp_id).setStyle('display','none');
		}
		if($type($('filter_salary_from'))){
			$('div-filter-salary-'+position).setStyle('display','none');
		}
	}
	
}
function focus_keyword(default_value)
{
	if ($('filter_keyword').value==default_value)
	{
		$('filter_keyword').value = '';
	}
	$('filter_keyword').setStyle('color','#000000');
}
function lost_focus_keyword(default_value)
{
	if ($('filter_keyword').value=='')
	{
		$('filter_keyword').setStyle('color','#858783');
		$('filter_keyword').value = default_value;
	}
}
function NumberOnly(ele) {
		if (!/^\d*$/.test(ele.value)) {
			ele.value = ele.value.replace(/[^\d]/g,"")
		}
	}
function ad_replace(start_str,end_str,str,replace_str)
{
	pos1 = str.indexOf(start_str);
	pos2 = str.indexOf(end_str,pos1+1);
	if ((pos1==-1)||(pos2==-1))
		return str;
	substring = str.substr(pos1,pos2-pos1+1);
	return str.replace(substring,replace_str);
}